package cn.tedu.csmall.product.web;

import cn.tedu.csmall.product.ex.ServiceException;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult<T> implements Serializable {
    @ApiModelProperty("错误信息")
    private String message;
    @ApiModelProperty("业务状态码")
    private Integer state;

    //操作“成功”响应的数据
    @ApiModelProperty("数据")
    private T data;

    public static JsonResult<Void> ok(){
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult jsr = new JsonResult();
        jsr.setState(ServiceCode.OK.getValue());
        jsr.setData(data);
        return jsr;
    }

    public static JsonResult<Void> fail(ServiceCode serviceCode,String message){
        JsonResult jsr = new JsonResult();
        jsr.setState(serviceCode.getValue());
        jsr.setMessage(message);
        return jsr;
    }

    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getServiceCode(),e.getMessage());
    }
}
